from django.http import HttpResponse
from django.shortcuts import render
from bookstore.models import Book
from bookstore.models import Author


def index(request):
    obj = Book.objects.create(title='三国志', pub='中汉末年', price=100, market_price=200)
    return HttpResponse('已添加')


def regiest(request):
    if request.method == 'GET':
        return render(request, 'reg_1.html')
    elif request.method == "POST":
        uname = request.POST['uname'].replace(" ", "")
        upwd = request.POST['upwd'].replace(" ", "")
        upwd_2 = request.POST['upwd_2'].replace(" ", "")
        sex = request.POST['sex'].replace(" ", "")
        email = request.POST['email'].replace(" ", "")
        phone = request.POST['phone'].replace(" ", "")
        if uname == "root" or uname == "super":
            return HttpResponse('账号昵称违规，请更换<a href="/regiest">返回重试</a>')
        elif upwd != upwd_2:
            return HttpResponse('两次密码输入不一致<a href="/regiest">返回重试</a>')
        else:
            obj = Author.objects.create(name=uname,
                                        sex=sex,
                                        password=upwd,
                                        email=email,
                                        phone=phone)
            return HttpResponse('注册成功')
        """
                将密码加密，然后将用户数据存入数据库
                """
    else:
        return HttpResponse("小破站正在维护，暂时不能注册")


def login(request):
    if request.method == 'GET':
        return render(request, 'log_1.html')
    elif request.method == 'POST':
        uname = request.POST['uname'].replace(" ", "")
        pwd = request.POST['pwd'].replace(" ", "")
        if uname != "root":
            return HttpResponse('该用户不存在<a href="http://127.0.0.1:8000/login">返回重试</a>')
        elif pwd != '123':
            return HttpResponse('密码错误<a href="http://127.0.0.1:8000/login">返回重试</a>')
        else:
            return render(request, 'log_2.html')
    else:
        return HttpResponse("访问出错")


def block_1(request):
    return render(request, 'block1.html')


def block_2(request):
    return render(request, 'block2.html')
